//+build windows
package utils

import (
	"os/exec"
	"syscall"
	"unsafe"
)

const (
	SW_HIDE = 0
	SW_SHOW = 5
)

func ShowWindow(show bool) {
	kdll, err := syscall.LoadDLL("kernel32.dll")
	if err != nil {
		return
	}
	defer kdll.Release()

	GetConsoleTitle, err := kdll.FindProc("GetConsoleTitleW")
	if err != nil {
		return
	}
	name := make([]uint16, 300)
	n, _, errno := GetConsoleTitle.Call(uintptr(unsafe.Pointer(&name[0])), uintptr(uint32(len(name))))
	if n == 0 {
		err = errno
		return
	}
	//name := string(utf16.Decode(uname[0:n]))

	dll, err := syscall.LoadDLL("user32.dll")
	if err != nil {
		return
	}
	defer dll.Release()

	FindWindow, err := dll.FindProc("FindWindowW")
	if err != nil {
		return
	}

	ShowWindow, err := dll.FindProc("ShowWindow")
	if err != nil {
		return
	}

	hwnd, _, errno := FindWindow.Call(uintptr(0), uintptr(unsafe.Pointer(&name[0])))
	if hwnd == 0 {
		err = errno
		return
	}

	if show {
		ShowWindow.Call(uintptr(hwnd), uintptr(SW_SHOW))
	} else {
		ShowWindow.Call(uintptr(hwnd), uintptr(SW_HIDE))
	}
}

//重启系统
func Reboot() (err error) {
	cmd := exec.Command("shutdown", "-r", "-t", "10")
	return cmd.Start()
}

//func test() {
//	cmd := exec.Command("2.exe")
//	cmd.SysProcAttr = new(syscall.SysProcAttr)
//	cmd.SysProcAttr.HideWindow = true
//	cmd.Start()
//}
